package com.wechart.service.system.impl;

import com.wechart.dao.domain.AreainfoCriteria;
import com.wechart.dao.domain.Bankinfo;
import com.wechart.dao.domain.BankinfoCriteria;
import com.wechart.dao.mapper.AreainfoMapper;
import com.wechart.dao.mapper.BankinfoMapper;
import com.wechart.model.BankInfoModel;
import com.wechart.model.SelectModel;
import com.wechart.model.SystemModel;
import com.wechart.service.system.BankinfoService;
import com.wechart.utils.DateUtil;
import com.wechart.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service(value = "bankinfoService")
public class BankinfoServiceImpl implements BankinfoService {

    @Autowired
    private BankinfoMapper bankinfoMapper;

    @Autowired
    private AreainfoMapper areainfoMapper;

    /**
     * 银行信息全查询
     * @param systemModel
     * @return
     */
    @Override
    public List<Bankinfo> getBankinfo(SystemModel systemModel){
        BankinfoCriteria bankinfoCriteria=new BankinfoCriteria();
        BankinfoCriteria.Criteria criteria=bankinfoCriteria.createCriteria();
        if(StringUtils.isNoNullString(systemModel.getBankcode())){
            criteria.andBankcodeEqualTo(systemModel.getBankcode());
        }
        if(StringUtils.isNoNullString(systemModel.getBankname())){
            criteria.andBanknameEqualTo(StringUtils.getNamefromString(systemModel.getBankname()));
        }
        if(StringUtils.isNoNullString(systemModel.getBankstat())){
            criteria.andBankstatEqualTo(Short.parseShort(systemModel.getBankstat()));
        }
        //银行类型
        if(StringUtils.isNoNullString(systemModel.getBanktype())){
            criteria.andBanktypeEqualTo(systemModel.getBanktype());
        }
        //清算行
        if(StringUtils.isNoNullString(systemModel.getDreccode())){
            criteria.andDreccodeLike(systemModel.getDreccode());
        }
        if (null!=systemModel.getAreacode() && !systemModel.getAreacode().equals("3300")){
            criteria.andAreacodeEqualTo(systemModel.getAreacode());
        }
        bankinfoCriteria.setOrderByClause("id asc");
        List<Bankinfo> data = new ArrayList<>();
        for (Bankinfo bankinfo:bankinfoMapper.selectByExample(bankinfoCriteria)){
            AreainfoCriteria areainfoCriteria = new AreainfoCriteria();
            areainfoCriteria.createCriteria().andCodeEqualTo(bankinfo.getAreacode());
            bankinfo.setAreacode(areainfoMapper.selectByExample(areainfoCriteria).get(0).getName());
            data.add(bankinfo);
        }
        return data;
    }

    /**
     * 银行信息插入
     * @param bankInfoModel
     * @return
     */
    @Override
    public int insertBankinfo(BankInfoModel bankInfoModel) {
        BankinfoCriteria bankinfoCriteria=new BankinfoCriteria();
        Bankinfo data = new Bankinfo();
//        data.setAreacode(bankInfoModel.getAreacode());
        data.setAreacode(bankInfoModel.getAreacode());
        data.setBankcode(bankInfoModel.getBankcode());
        data.setBanklevel(bankInfoModel.getBanklevel());
        data.setBankname(bankInfoModel.getBankname());
        data.setBankstat(bankInfoModel.getBankstat());
        data.setBanktype(bankInfoModel.getBanktype());
        data.setContact(bankInfoModel.getContact());
        data.setDreccode(bankInfoModel.getDreccode());
        switch (bankInfoModel.getBankstat()) {
            case 0:
                data.setEffectdate("");
                data.setExpiredate(DateUtil.formate(new Date(System.currentTimeMillis()),"SHORT"));
            case 1:
                data.setEffectdate(DateUtil.formate(new Date(System.currentTimeMillis()),"SHORT"));
                data.setExpiredate("");
            case 9:
                data.setEffectdate("");
                data.setExpiredate("");
        }
//        data.setEffectdate(bankInfoModel.getEffectdate());
//        data.setExpiredate(bankInfoModel.getExpiredate());
        data.setModifydate(DateUtil.formate(new Date(System.currentTimeMillis()),"SHORT"));
        data.setRegion(bankInfoModel.getRegion());
        data.setSupercode(bankInfoModel.getSupercode());
        data.setSvclist(bankInfoModel.getSvclist());
        data.setTel(bankInfoModel.getTel());

        return bankinfoMapper.insertSelective(data);
    }

    /**
     * 银行信息修改
     * @param bankInfoModel
     * @return
     */
    @Override
    public int modifyBankinfo(BankInfoModel bankInfoModel ,int id) {
        BankinfoCriteria bankinfoCriteria=new BankinfoCriteria();
        bankinfoCriteria.createCriteria().andIdEqualTo(id);
        Bankinfo data = new Bankinfo();

        data.setAreacode(bankInfoModel.getAreacode());
        data.setBankcode(bankInfoModel.getBankcode());
        data.setBanklevel(bankInfoModel.getBanklevel());
        data.setBankname(bankInfoModel.getBankname());
        data.setBankstat(bankInfoModel.getBankstat());
        data.setBanktype(bankInfoModel.getBanktype());
        data.setContact(bankInfoModel.getContact());
        data.setDreccode(bankInfoModel.getDreccode());
        switch (bankInfoModel.getBankstat()) {
            case 0:
//                data.setEffectdate("");
                data.setExpiredate(DateUtil.formate(new Date(System.currentTimeMillis()),"SHORT"));
            case 1:
                data.setEffectdate(DateUtil.formate(new Date(System.currentTimeMillis()),"SHORT"));
//                data.setExpiredate("");
            case 9:
//                data.setEffectdate("");
//                data.setExpiredate("");
        }
//        data.setEffectdate(bankInfoModel.getEffectdate());
//        data.setExpiredate(bankInfoModel.getExpiredate());
        data.setModifydate(DateUtil.formate(new Date(System.currentTimeMillis()),"SHORT"));
        data.setRegion(bankInfoModel.getRegion());
        data.setSupercode(bankInfoModel.getSupercode());
        data.setSvclist(bankInfoModel.getSvclist());
        data.setTel(bankInfoModel.getTel());

        return bankinfoMapper.updateByExampleSelective(data,bankinfoCriteria);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Bankinfo getBankinfoById(int id) {
        BankinfoCriteria bankinfoCriteria=new BankinfoCriteria();
        bankinfoCriteria.createCriteria().andIdEqualTo(id);
        List<Bankinfo> data = bankinfoMapper.selectByExample(bankinfoCriteria);
        if (null!=data) {
            return data.get(0);
        }else{
            return null;
        }
    }

    /**
     * 取得银行名称
     * @param name
     * @return
     */
    @Override
    public List<SelectModel> selectBankname(String name){
        BankinfoCriteria bankinfoCriteria=new BankinfoCriteria();
        BankinfoCriteria.Criteria criteria=bankinfoCriteria.createCriteria();
        if(StringUtils.isNoNullString(name)){
            criteria.andBanknameLike("%"+name+"%");
        }
        bankinfoCriteria.setDistinct(true);
        List<Bankinfo> list=bankinfoMapper.selectByExample(bankinfoCriteria);
        List<SelectModel> data = new ArrayList<>();
        SelectModel model=null;
        for(Bankinfo bankinfo:list){
            model=new SelectModel();
            model.setText(bankinfo.getBankcode()+"-"+bankinfo.getBankname());
            model.setId(bankinfo.getBankcode()+"-"+bankinfo.getBankname());
            data.add(model);
        }
        return data;
    }

    public int insertBankinfoByArray(String[] fieldsTmp){
        Bankinfo data=new Bankinfo();
        data.setBankcode(fieldsTmp[0]);
        data.setBankname(fieldsTmp[1]);
        data.setBanktype(fieldsTmp[2]);
        data.setBanklevel(fieldsTmp[3]);
        data.setSupercode(fieldsTmp[4]);
        data.setDreccode(fieldsTmp[5]);
        data.setRegion(fieldsTmp[6]);
        data.setAreacode(fieldsTmp[7]);
        data.setSvclist(fieldsTmp[8]);
        data.setContact(fieldsTmp[9]);
        data.setTel(fieldsTmp[10]);
        data.setBankstat(Short.parseShort(fieldsTmp[11]));
        data.setEffectdate(fieldsTmp[12]);
        data.setExpiredate(fieldsTmp[13]);
        data.setModifydate(fieldsTmp[14]);
        return bankinfoMapper.insertSelective(data);
    }


}
